package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.DeptListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.DeptContent;
import com.isunland.managesystem.entity.EnterpriseList;
import java.util.ArrayList;
import java.util.HashMap;

public class DeptListFragment extends BaseListFragment {
  private ArrayList<DeptContent> mlist;
  private DeptListAdapter mAdapter;

  @Override public String getUrl() {
    return ApiConst.URL_SENDMASSAGE_PEOPLE;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("type", "org");
    params.put("orgCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    return params;
  }

  @Override public void resolveResponse(String json) {
    DeptContent[] contens = new Gson().fromJson(json, DeptContent[].class);
    setAdapter(contens);
  }

  private void setAdapter(DeptContent[] contens) {
    if (mAdapter == null) {
      mAdapter = new DeptListAdapter(getActivity(), contens);
    }
    setListAdapter(mAdapter);
    ((DeptListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    DeptContent content = mAdapter.getItem(position - 1);
    EnterpriseList item = new EnterpriseList();
    item.setId(content.getId());
    item.setEnterpriseName(content.getName());

    Intent i = new Intent();
    i.putExtra(EnterpriseListFragment.EXTRA_ENTERPRISE, item);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();

    super.onListItemClick(l, v, position, id);
  }
}
